package yt.kpool.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;

import yt.kpool.adapter.ProfitTypeListAdapter;

import yt.kpool.bean.BaseBean;
import yt.kpool.bean.CurrencyList;

import yt.kpool.bean.CurrencyListBean;
import yt.kpool.bean.ProfitTypeBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.imageUtil.GlideUtil;
import yt.kpool.view.dialog.ContentDialog;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.StopPayDialog;
import yt.kpool.view.dialog.SuccessDialog;
import yt.kpool.view.pop.CurrencyPopupWindow;

/**
 * 收益模式
 */
public class ProfitTypeActivity extends AppCompatActivity {

    @BindView(R.id.ll_currency)
    LinearLayout ll_currency;
    @BindView(R.id.tv_currrncyname)
    TextView tv_currrncyname;
    @BindView(R.id.iv_curreney)
    ImageView iv_curreney;
    @BindView(R.id.tv_name)
    TextView tv_name;

    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;

    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<ProfitTypeBean.DataBean.ProfitTypeBean2> dataBeanList;
    private ProfitTypeListAdapter profitTypeListAdapter;//收益模式列表

    private List<CurrencyList.DataBean> currencyListData;
    private CurrencyPopupWindow currencyPopupWindow;//币种选择
    private LoadingDialog loadingDialog;
    private int currencyId;//币种id
    private String currencycode = "";//币种code

    private List<ProfitTypeBean.DataBean> profitTypeBeanData;
    public static void start(Activity mactivity, String ChildAccountID, String ChildAccountName) {
        Intent intent = new Intent(mactivity, ProfitTypeActivity.class);
        intent.putExtra("ChildAccountID", ChildAccountID);
        intent.putExtra("ChildAccountName", ChildAccountName);
        mactivity.startActivity(intent);
    }

    public String getChildAccountID() {
        return getIntent().getStringExtra("ChildAccountID");
    }

    public String getChildAccountName() {
        return getIntent().getStringExtra("ChildAccountName");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profit_type);
        ButterKnife.bind(this);
        init();
        initList();
        if (NetWorkUtil.checkNet(ProfitTypeActivity.this)) {
            getData(0, true);
        } else {

            ToastUtil.showToast(ProfitTypeActivity.this, getResources().getString(R.string.internet_error));
        }
    }


    /**
     * 币种选择pop初始化
     */
    private void initCurrencyPopupWindow() {
        currencyPopupWindow = new CurrencyPopupWindow(currencyListData, this, new CurrencyPopupWindow.OnSeleCurrencyLayoutListener() {
            @Override
            public void selectHouseLayout(int i) {
                currencyId = currencyListData.get(i).getId();
                currencycode = currencyListData.get(i).getCode();
                GlideUtil.loadImage(ProfitTypeActivity.this, NetHelperNew.URL + currencyListData.get(i).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                tv_currrncyname.setText(currencyListData.get(i).getCode());
                dataBeanList.clear();
                profitTypeListAdapter.notifyDataSetChanged();
                //更新
                dataBeanList.addAll(profitTypeBeanData.get(i).getProfitType());
                profitTypeListAdapter.notifyDataSetChanged();

            }
        });
    }

    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({R.id.ll_currency, R.id.ll_finish, R.id.tv_update})
    public void onClick(View view) {
        switch (view.getId()) {


            case R.id.ll_finish:

                finish();
                break;
            case R.id.tv_update:
                //更改收益模式
                new ContentDialog(this, "确认更改收益模式？", "通知", new ContentDialog.OnSuccessListener() {
                    @Override
                    public void sure() {
                        update();
                    }
                }).show();

                break;
            case R.id.ll_currency:
                //选择币种
                if (currencyPopupWindow != null)
                    currencyPopupWindow.showAsDropDown(ll_currency, 0, 0);
                break;

        }
    }

    /**
     * 更改收益模式
     */
    private void update() {
        String name = dataBeanList.get(profitTypeListAdapter.getPos()).getName();
        if (NetWorkUtil.check(this)) {

            loadingDialog.show();
            NetHelperNew.ChangeChildAccountProfitType(currencyId + "", getChildAccountID(), "" + name, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "更改收益模式结果" + data);

                    loadingDialog.dismiss();


                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);


                    if (baseBean.getType() == 1) {


                        //更新数据
                        new SuccessDialog(ProfitTypeActivity.this, baseBean.getContent(), new SuccessDialog.OnSuccessListener() {
                            @Override
                            public void getResult() {
                                refreshClear();
                                EventBus.getDefault().post("456");
                            }
                        }).show();


                    } else {
                        new ErrorDialog(ProfitTypeActivity.this, baseBean.getContent()).show();
                    }


                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);

                    loadingDialog.dismiss();
                    mRecyclerView.refreshComplete(20);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });
        }

    }

    private void init() {
        loadingDialog = new LoadingDialog(this, 0);
        tv_name.setText(getChildAccountName());
    }

    /**
     * 列表初始化
     */
    private void initList() {

        loadingDialog = new LoadingDialog(this, 0);
        dataBeanList = new ArrayList<>();
        currencyListData=new ArrayList<>();
        profitTypeListAdapter = new ProfitTypeListAdapter(this, dataBeanList);

        lRecyclerViewAdapter = new LRecyclerViewAdapter(profitTypeListAdapter);


        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.white);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);

        //分割线
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.d_1)
                .setPadding(R.dimen.d_0)
                .setColorResource(R.color.colorLine)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {

                refreshClear();
            }
        });
        //停止下拉刷新
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadMoreEnabled(false);


    }


    private void getData(int pageIndex, final boolean isLoading) {

        if (NetWorkUtil.check(this)) {
            if (isLoading)
                loadingDialog.show();
            NetHelperNew.ChildAccountProfitType(currencyId + "", getChildAccountID(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "我的收益模式" + data);
                    if (isLoading)
                        loadingDialog.dismiss();


                    ProfitTypeBean profitTypeBean = MyGson.getInstance().fromJson(data, ProfitTypeBean.class);

                    profitTypeBeanData = profitTypeBean.getData();
                    if (profitTypeBean.getType() == 1) {
                        if (profitTypeBeanData.size() > 0) {


                            for (int i = 0; i < profitTypeBeanData.size(); i++) {
                                CurrencyList.DataBean dataBean = new CurrencyList.DataBean();
                                ProfitTypeBean.DataBean dataBean1 = profitTypeBeanData.get(i);
                                dataBean.setCode(dataBean1.getCode());
                                dataBean.setIcon(dataBean1.getIcon());
                                dataBean.setId(dataBean1.getId());
                                dataBean.setName(dataBean1.getName());
                                currencyListData.add(dataBean);
                            }

                            initCurrencyPopupWindow();
                            if (currencyListData.size() > 0) {
                                currencycode =currencyListData.get(0).getCode();
                                currencyId = currencyListData.get(0).getId();
                                GlideUtil.loadImage(ProfitTypeActivity.this, NetHelperNew.URL + currencyListData.get(0).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                                tv_currrncyname.setText(currencyListData.get(0).getCode());
                                mRecyclerView.refreshComplete(20);
                                //更新数据
                                dataBeanList.addAll(profitTypeBeanData.get(0).getProfitType());
                                for (int i = 0; i < dataBeanList.size(); i++) {
                                    if (dataBeanList.get(i).isActivate()) {
                                        profitTypeListAdapter.setPos(i);
                                        break;
                                    }
                                }

                                profitTypeListAdapter.notifyDataSetChanged();

                            }


                        }
                    } else {
                        new ErrorDialog(ProfitTypeActivity.this, profitTypeBean.getContent()).show();
                    }


                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    if (isLoading)
                        loadingDialog.dismiss();
                    mRecyclerView.refreshComplete(20);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });
        }

    }

    /**
     * 刷新数据
     */
    private void refreshClear() {
        dataBeanList.clear();
        profitTypeListAdapter.notifyDataSetChanged();
        currencyListData.clear();

        getData(0, true);
    }
}
